package com.trs.publish.Constants;

import java.util.Date;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.trs.publish.domain.Dict;
import com.trs.publish.domain.DictType;
import com.trs.publish.repository.DictMapper;
import com.trs.publish.repository.DictTypeMapper;
import com.trs.publish.util.common.MySysUtils;
/**
 * 字典表服务
 * @author chenming
 *
 */
@Component
public class DictService implements CommandLineRunner{
	@Autowired
	private DictTypeMapper dictTypeMapper;
	@Autowired
	private DictMapper dictMapper;
    /**
     *      所有的数据字典类型  
     */
    private static Map<String,DictType> dictTypeMap = new HashMap<String, DictType>();
    /**
     *  所有类型，对应的数据字典项
     */
    private static Map<String, List<Dict>> dictListMap = new HashMap<String,List<Dict>>();
            
    /**
     * 类型与字典项  由List 转成Map（key,value）
     */
    private static Map<String, Map<String,String>> dictMapMap = new HashMap<>();
    /**
     * 用字典类型查出字典
     * @param dictTypeName 字典类型名： dlFocusInfo.status
     * @return 该字典类型对应的所有字典
     */
    public List<Dict> getDictListByType(String dictTypeName){
    	return dictListMap.get(dictTypeName);
    }
    /**
     * 使用字典类型和字典名称查出字典内容
     * @param dictTypeName 字典类型名： dlFocusInfo.status
     * @param dictName 字典名： wait 
     * @return 字典内容： 等待处理
     */
    public String getDictContentByTypeAndName(String dictTypeName,String dictName){
    	return dictMapMap.get(dictTypeName).get(dictName);
    }
    /**
     * 通过字典类型名取得字典类型的内容
     * @param dictTypeName 自带你类型名：dlFocusInfo.status
     * @return 字典类型内容： 关注人员状态
     */
    public String getDictTypeContentByName(String dictTypeName) {
    	return dictTypeMap.get(dictTypeName).getDtContent();
    }
    /**
     * 取得字典类型map
     * @return
     */
    public Map<String,DictType> getDictTypeMap() {
    	return dictTypeMap;
    }
    
	@Override
	public void run(String... args) throws Exception {
		//初始化字典表
//		initDict();
		List<DictType> dtList = dictTypeMapper.selectAllCascade();
		recombineDict(dtList);
	}
	/**
	 * 重组字典表使其方便查询
	 * @param dtList
	 */
	private void recombineDict(List<DictType> dtList) {
		dtList.forEach(dt->{
			dictTypeMap.put(dt.getDtName(), dt);
			dictListMap.put(dt.getDtName(), dt.getDictList());
			Map<String,String> dictMap = dt.getDictList().stream()
					.collect(Collectors.toMap(Dict::getDictName,Dict::getDictContent));
			dictMapMap.put(dt.getDtId(), dictMap);
		});
	}
	/**
	 * 初始化字典表
	 */
	private void initDict() {
		String[] dlFocusInfoStatus = new String[] {"wait","handled"};
		String[] dlFocusInfoStatusContent = new String[] {"等候处理","处理完毕"};
		DictType dictType = new DictType();
		dictType.setCreateTime(new Date());
		dictType.setDtId(MySysUtils.createUUID());
		dictType.setDtName("dlFocusInfo.status");
		dictType.setDtContent("关注人员状态");
		dictType.setDtRank(1);
		this.dictTypeMapper.insert(dictType);
		for(int i=0;i<dlFocusInfoStatus.length;i++) {
			Dict dic = new Dict();
			dic.setCreateTime(new Date());
			dic.setDictName(dlFocusInfoStatus[i]);
			dic.setDictContent(dlFocusInfoStatusContent[i]);
			dic.setDtId(dictType.getDtId());
			Integer rank = this.dictMapper.selectMaxRank();
			rank = rank==null?1:rank+1;
			dic.setDictRank(rank);
			dic.setDictId(MySysUtils.createUUID());
			this.dictMapper.insert(dic);
		}
	}
}
